#include <iostream>
#include <thread>
#include <stop_token>

void worker(std::stop_token token) {
    // 注册停止回调
    std::stop_callback callback(token, []() {
        std::cout << "Callback: Cleaning up resources...\n";
    });

    while (!token.stop_requested()) {
        std::cout << "Worker is working...\n";
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
    }
    std::cout << "Worker is stopping...\n";
}

int main() {
    std::stop_source source;
    std::stop_token token = source.get_token();

    std::thread t(worker, token);

    std::this_thread::sleep_for(std::chrono::seconds(2));
    source.request_stop(); // 发出停止请求
    t.join();
    return 0;
}
